package com.winning.joiner.magiccube.common.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.winning.joiner.magiccube.common.dto.ResponseModel;

@ControllerAdvice
public class DefaultExceptionHandler {
	private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionHandler.class);

	/**
	 * 拦截未知的运行时异常
	 */
	@ExceptionHandler(RuntimeException.class)
	public ResponseModel notFount(RuntimeException e)
	{
		LOGGER.error("运行时异常:", e);
		return ResponseModel.error("运行时异常:" + e.getMessage());
	}

	/**
	 * 系统异常
	 */
	@ExceptionHandler(Exception.class)
	public ResponseModel handleException(Exception e)
	{
		LOGGER.error(e.getMessage(), e);
		return ResponseModel.error("服务器错误，请联系管理员");
	}
	
	/**
     * 请求方式不支持
     */
    @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
    public ResponseModel handleException(HttpRequestMethodNotSupportedException e)
    {
    	LOGGER.error(e.getMessage(), e);
        return ResponseModel.error("不支持' " + e.getMethod() + "'请求");
    }



}
